package com.smzd.projforge.actions;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.core.actions.AbstractScopedController;
import com.smzd.core.annotations.Access;
import com.smzd.core.business.AbstractService;
import com.smzd.core.utils.db.SRPage;
import com.smzd.core.utils.json.JsonHelper;
import com.smzd.projforge.business.MTMRelationService;
import com.smzd.projforge.domain.MTMRelation;
import com.smzd.projforge.domain.Project;

@Controller
@Access
@RequestMapping("/project/{scopeId}/mtmrelations")
public class MTMRelationController extends
		AbstractScopedController<MTMRelation, Project> {
	@Autowired
	private MTMRelationService service;

	@Override
	protected AbstractService<MTMRelation> getService() {
		return service;
	}

	@Override
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String toAddPage(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, MTMRelation elem,
			BindingResult result) throws Exception {
		return super.toAddPage(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String postAddData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, MTMRelation elem,
			BindingResult result) throws Exception {
		initScope(scopeId, elem);
		if(elem.getCreateMode()==MTMRelation.CREATE_MODE_FIELDS){
			service.insertByMSFields(elem);
		}else
		if(elem.getCreateMode()==MTMRelation.CREATE_MODE_CLASSES){
			service.insertByMSClasses(elem);
		}else{
			throw new Exception("提交数据不满足条件");
		}
		return JsonHelper.getSuccessJsonString("id", elem.getId());
	}

	@Override
	@RequestMapping(method = RequestMethod.GET)
	public String toListPage(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, MTMRelation elem,
			BindingResult result) throws Exception {
		return super.toListPage(model, scopeId, elem, result);
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String getListData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId,
			SRPage<Project, MTMRelation> paging, BindingResult result)
			throws Exception {
		return super.getListData(model, scopeId, paging, result);
	}

	@Override
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String toEditPage(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, MTMRelation elem,
			BindingResult result) throws Exception {
		elem = service.getItemById(elem.getId());
		return super.toEditPage(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public String postEditData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, MTMRelation elem,
			BindingResult result) throws Exception {
		return super.postEditData(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/remove")
	@ResponseBody
	public String postRemoveData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, MTMRelation elem,
			BindingResult result) throws Exception {
		return super.postRemoveData(model, scopeId, elem, result);
	}

	@Override
	protected Project getScope(int scopeId) {
		Project project = new Project();
		project.setId(scopeId);
		return project;
	}

}
